package cn.edu.buu.hospital.web;
import cn.edu.buu.hospital.entity.Visit;
import cn.edu.buu.hospital.pojo.MedicalRecord;
import cn.edu.buu.hospital.service.MedicalRecordService;
import cn.edu.buu.hospital.service.VisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpSession;
import java.util.Date;
@Controller
@RequestMapping("/doctor/medicalRecord")
public class MedicalRecordController {
    @Autowired
    private VisitService visitService;//获取就诊记录信息
    @Autowired
    private MedicalRecordService medicalRecordService;//
    /**
     * 跳转到写病历单页面
     */
    @GetMapping("/write/{visitId}")
    public String writeMedicalRecord(@PathVariable("visitId") Long visitId, HttpSession session, Model model) {
        // 验证医生登录状态
        Object doctorObj = session.getAttribute("loginDoctor");
        if (doctorObj == null) {
            return "redirect:/toDoctorLogin";
        }

        // 获取就诊记录信息
        Visit visit = visitService.findById(visitId);
        if (visit == null) {
            model.addAttribute("error", "找不到相关就诊记录");
            return "redirect:/doctor/patients";
        }

        model.addAttribute("visit", visit);
        return "backend/doctor/writeMedicalRecord";
    }

    /**
     * 提交病历单
     */
    @PostMapping("/submit")
    public String submitMedicalRecord(
            @RequestParam("visitId") Long visitId,
            @RequestParam String diagnosis,
            @RequestParam String treatment,
            @RequestParam String advice,
            HttpSession session,
            Model model) {
        
        // 验证医生登录状态
        Object doctorObj = session.getAttribute("loginDoctor");
        if (doctorObj == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 创建病历记录
        MedicalRecord record = new MedicalRecord();
        record.setVisitId(visitId.intValue()); // 将Long转换为Integer
        record.setDiagnosis(diagnosis);
        record.setTreatment(treatment);
        record.setAdvice(advice);
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        
        // 保存病历记录
        boolean result = medicalRecordService.insert(record);
        
        if (result) {
            // 只保存病历记录，不需要更新就诊记录状态
            
            model.addAttribute("success", "病历单提交成功");
        } else {
            model.addAttribute("error", "病历单提交失败，请重试");
        }
        
        return "redirect:/doctor/patients";
    }

    /**
     * 查看病历单详情
     */
    @GetMapping("/view/{visitId}")
    public String viewMedicalRecord(@PathVariable("visitId") Long visitId, HttpSession session, Model model) {
        // 验证医生登录状态
        Object doctorObj = session.getAttribute("loginDoctor");
        if (doctorObj == null) {
            return "redirect:/toDoctorLogin";
        }
        
        // 获取就诊记录信息
        Visit visit = visitService.findById(visitId);
        if (visit == null) {
            model.addAttribute("error", "找不到相关就诊记录");
            return "redirect:/doctor/patients";
        }
        
        // 获取病历记录 - 需要将Long转换为Integer
        MedicalRecord record = medicalRecordService.selectByVisitId(visitId.intValue());
        
        model.addAttribute("visit", visit);
        model.addAttribute("record", record);
        return "backend/doctor/patientDetail";
    }
}